﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CrabSS_M
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class settinginfo //把我整不会了😅
        {
            public string? WindowTitle { get; set; }
            public string? Server1 { get; set; }
            public string? Server2 { get; set; }
            public string? Server3 { get; set; }
            public string? Server4 { get; set; }
            public string? Server5 { get; set; }
        }
        public class RootObject
        {
            public string? data { get; set; }
            public string? retCode { get; set; }
            public string? time { get; set; }
            public string? uuid { get; set; }
            public string? id { get; set; }
            public string? password { get; set; }
            public string? perm { get; set; }
            public string? token { get; set; }
            public string? username { get; set; }
        }
        public MainWindow()
        {
            InitializeComponent();
            if (!File.Exists("settings.json"))
            {//初始化Json部分
                try
                {
                    settinginfo settings = new settinginfo();
                    string json = JsonMapper.ToJson(settings); //using LitJson
                    StreamWriter sw = new StreamWriter(System.Environment.CurrentDirectory + "\\settings.json");
                    sw.Write(json);
                    sw.Close();
                    string strJson = File.ReadAllText("settings.json", Encoding.UTF8);
                    JObject oJson = JObject.Parse(strJson); //using Newtonsoft.Json.Linq
                    oJson["WindowTitle"] = "CrabSS | 服务器管理中心";
                    oJson["Server1"] = null;
                    oJson["Server2"] = null;
                    oJson["Server3"] = null;
                    oJson["Server4"] = null;
                    oJson["Server5"] = null;
                    string strConvert = Convert.ToString(oJson); //将json装换为string
                    File.WriteAllText("settings.json", strConvert); //将内容写进json文件中
                    sw.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发生错误："+ex.ToString(), "关键错误", MessageBoxButton.OK, MessageBoxImage.Hand, MessageBoxResult.OK);
                    Close();
                }
            }
            else
            {
                settinginfo readed = new settinginfo();
                using (StreamReader readFile = File.OpenText("settings.json"))
                {
                    using (JsonTextReader reader = new JsonTextReader(readFile)) //using Newtonsoft.Json
                    {
                        JObject oJson = (JObject)JToken.ReadFrom(reader);
                        string title = readed.WindowTitle = oJson["WindowTitle"].ToString();
                        string Server1 = readed.Server1 = oJson["Server1"].ToString();
                        string Server2 = readed.Server2 = oJson["Server2"].ToString();
                        string Server3 = readed.Server3 = oJson["Server3"].ToString();
                        string Server4 = readed.Server4 = oJson["Server4"].ToString();
                        string Server5 = readed.Server5 = oJson["Server5"].ToString();
                        reader.Close();
                        serverlist.Items.Add(Server1);
                        serverlist.Items.Add(Server2);
                        serverlist.Items.Add(Server3);
                        serverlist.Items.Add(Server4);
                        serverlist.Items.Add(Server5);
                    }
                }
            }
            logind.Visibility = Visibility.Hidden;
            gg.Content = lingqi.GetHttpResponse("https://v6.crabapi.cn/api/crabss/broadcast?channel=beta&encode=text", 1600);
            hiko.Content = lingqi.GetHttpResponse("https://v1.hitokoto.cn/?encode=text", 1600);
        }
        private void login_Click(object sender, RoutedEventArgs e)
        {
            Dictionary<string, string> postData = new Dictionary<string, string>();
            postData.Add("token", tokenbox.Password);
            string b = lingqi.Post("https://v6.crabapi.cn/api/session", postData);
            RootObject rb = JsonConvert.DeserializeObject<RootObject>(b);
            if (rb.retCode == null)
            {
                username.Content = rb.username + " (UID:"+rb.id+")";
                token.Content = rb.token;
                notlogin.Visibility = Visibility.Hidden;
                logind.Visibility= Visibility.Visible;
            }
            else
            {
                MessageBox.Show(rb.data, "未能登陆...", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void runserver_Click(object sender, RoutedEventArgs e)
        {
            object p = serverlist.SelectedItem;
            if (p==null)
            {
                MessageBox.Show("？恁搁这耍猴呢？", "😅", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (p == "")
            {
                MessageBox.Show("这不是服务器啊宝，右下角创建一个", "p 是 null", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                MessageBox.Show((string)p);
            }
        }

        private void addserver_Click(object sender, RoutedEventArgs e)
        {
            CreateNewServer c = new();
            c.Show();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
